<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/28
 * Time: 14:21
 */

namespace app\admin\controller;


use app\admin\model\ArticleModel;
use app\admin\model\LabelsModel;
use think\Request;
use think\Config;

class Article extends Base
{
    protected $articleModel;
    protected $labelsModel;
    protected $labelsMaps;

    /*
     * 依赖注入Model
     * */
    public function __construct(ArticleModel $articleModel,LabelsModel $labelsModel){
        parent::__construct();
        $this->articleModel = $articleModel;
        $this->labelsModel = $labelsModel;
        $this->labelsMaps=[
            "status" => 1,
            "category" => 'article'
        ];
    }

    /*
     * 列表
     * */
    public function article_index(){
        $this->assign("data",$this->articleModel->listAll());
        return $this->fetch('article/index');
    }

    /*
    * 新增
    * */
    public function article_add(){
        if(Request::instance()->isPost()){
            $validate=$this->articleModel->validate_model('add');
            if ($validate['code']){
                return $this->articleModel->add();
            }else{
                return $validate;
            }
        }
        $this->assign("labels",$this->labelsModel->getByMaps($this->labelsMaps));
        return $this->fetch('article/add');
    }

    /*
    * 修改
    * */
    public function article_edit(){
        if(Request::instance()->isPost()){
            $validate=$this->articleModel->validate_model('edit');
            if ($validate['code']){
                return $this->articleModel->edit();
            }else{
                return $validate;
            }
        }
        $id=input('get.id/d');
        $this->assign("labels",$this->labelsModel->getByMaps($this->labelsMaps));
        $this->assign("data",$this->articleModel->getById($id));
        return $this->fetch('article/edit');
    }

    /*
    * 删除
    * */
    public function article_del(){
        return $this->articleModel->del();
    }


}